﻿Imports System.Data
Imports System.Data.OleDb
Imports geraLaudosAutomatico.conexao
Imports System.Text
Imports System.Data.SqlClient

Public Class departamentoDAO


    Dim sql As New StringBuilder
    Dim objConn As New conexao
    Dim i As Integer
    Dim departamento As New departamentoVO



    Public Function getDepartamentos() As Generic.List(Of departamentoVO)

        sql.Clear()
        sql.Append("SELECT *FROM DEPARTAMENTO ORDER BY ID_DEPARTAMENTO")

        Dim oReader As OleDbDataReader

        Dim lista As New Generic.List(Of departamentoVO)



        Try
            Dim oComando As New OleDbCommand(sql.ToString, objConn.abreConexao)

            oReader = oComando.ExecuteReader

            While oReader.Read

                departamento = New departamentoVO
                departamento.ID_DEPARTAMENTO = oReader("ID_departamento")
                departamento.NUMERO_DEPARTAMENTO = oReader("NUMERO_DEPARTAMENTO")
                departamento.CIDADE_DEPARTAMENTO = oReader("CIDADE_DEPARTAMENTO")
                departamento.STATUS = oReader("STATUS")
                lista.Add(departamento)

            End While

            Return lista

        Catch ex As Exception

            Throw ex

        Finally

            objConn.fechaConexao()

        End Try

    End Function



    Public Function getdepartamento(ByVal id_departamento As Integer) As departamentoVO

        sql.Clear()
        sql.Append("SELECT *FROM departamento WHERE ID_departamento=" & id_departamento)

        Dim oReader As OleDbDataReader

        Try
            Dim oComando As New OleDbCommand(sql.ToString, objConn.abreConexao)

            oReader = oComando.ExecuteReader

            While oReader.Read

                departamento = New departamentoVO
                departamento.ID_DEPARTAMENTO = oReader("ID_departamento")
                departamento.NUMERO_DEPARTAMENTO = oReader("NUMERO_DEPARTAMENTO")
                departamento.CIDADE_DEPARTAMENTO = oReader("CIDADE_DEPARTAMENTO")
                departamento.STATUS = oReader("STATUS")


            End While

            Return departamento

        Catch ex As Exception

            Throw ex

        Finally

            objConn.fechaConexao()

        End Try

    End Function


    Public Function populaGridDestinos(ByVal grid As DataGridView) As BindingSource

        Dim objConn As New conexao
        Dim dataAdapter As New OleDbDataAdapter
        Dim bindingSource1 As BindingSource = Nothing

        Try
            Try
                sql.Clear()
                sql.Append("SELECT *FROM DEPARTAMENTO ORDER BY CIDADE_DEPARTAMENTO")
                dataAdapter = New OleDbDataAdapter(sql.ToString, objConn.abreConexao)
                Dim commandBuilder As New OleDbCommandBuilder(dataAdapter)
                '--------------------------------------------------------------------------
                ' Popula a tabela 
                '--------------------------------------------------------------------------
                Dim table As New DataTable()

                table.Locale = System.Globalization.CultureInfo.InvariantCulture

                dataAdapter.Fill(table)

                bindingSource1.DataSource = table


                grid.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
            Catch ex As SqlException
                generica.gravaErro(ex)
            End Try



        Catch ex As Exception

            generica.gravaErro(ex)

        End Try
        Return bindingSource1
    End Function

End Class
